//
//  SuperViewController.m
//  艾听歌
//
//  Created by dllo on 16/5/20.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "SuperViewController.h"
#import "SuperTableViewCell.h"
#import "SupDetailViewController.h"


#import "SuperStarModel.h"


#define Supercell @"cell"
@interface SuperViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) UITableView *superTableview;
@property (nonatomic, strong) NSMutableArray *superStarArray;
@property (nonatomic, strong) NSMutableArray *allDataArray;

@end

@implementation SuperViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.view.backgroundColor = [UIColor whiteColor];
    self.view.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff, 0x9c9c9c);
    self.navigationController.navigationBar.dk_barTintColorPicker = DKColorPickerWithRGB(0xFF6EB4, 0x9C9C9C);
    // 数组初始化
    self.superStarArray = [NSMutableArray array];
    
    // 创建tableView
    self.superTableview = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
    self.superTableview.delegate = self;
    self.superTableview.dataSource = self;
    [self.view addSubview:self.superTableview];
    // 注册
    [self.superTableview registerClass:[SuperTableViewCell class] forCellReuseIdentifier:Supercell];
    [self getDataHandle];
}
// 网络请求
- (void)getDataHandle
{
    
    NSString *urlStr = @"http://api.dongting.com/song/singer_classify/items??app=ttpod&v=v9.0.1.2016050614&user_id=0&mid=iPhone7%2C2&f=f320&s=s310&imsi=&hid=&splus=9.3.1&active=1&net=2&openudid=5e6106cc8d5bcf35f60a2997c79a00bb5a7c631f&idfa=ABDADD65-0877-4840-B14F-DFC8D1114AB0&utdid=VzR0dwnWYs8DANbYJ8SsFzOU&alf=(null)&bundle_id=com.ttpod.music&latitude=-1&longtitude=-1&access_token=(null)&vc=9000100";
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager.responseSerializer setAcceptableContentTypes:[NSSet setWithObjects:@"text/html", @"application/json", @"text/json", @"text/javascript",@"text/html",@"text/css", @"text/plain", @"application/x-javascript", @"application/javascript",nil]];
    [manager GET:urlStr parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        

        
        NSDictionary *dic = [responseObject objectForKey:@"data"];
        for (NSDictionary *smallDic in dic) {
            SuperStarModel *model = [[SuperStarModel alloc] init];
            [model setValuesForKeysWithDictionary:smallDic];
            [self.superStarArray addObject:model];
        }
        
        
        [self.superTableview reloadData];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        NSLog(@"%@",error);
    }];
}




- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    SuperStarModel *bigModel = [self.superStarArray objectAtIndex:section];
    
    NSMutableArray *array = bigModel.singerClassifyGroup;
    
    return array.count;
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
   
    return self.superStarArray.count;

}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    SuperTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:Supercell forIndexPath:indexPath];
    SuperStarModel *bigmodel = self.superStarArray[indexPath.section];
    cell.cellModel = bigmodel.singerClassifyGroup[indexPath.row];
    
    cell.dk_backgroundColorPicker = DKColorPickerWithRGB(0xffffff, 0x9c9c9c);
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    SupDetailViewController *supdetailVC = [[SupDetailViewController alloc] init];
    
    SuperStarModel *model = [self.superStarArray objectAtIndex:indexPath.section];
    NSMutableArray *array = model.singerClassifyGroup;
    SmallModel *smallModel = [array objectAtIndex:indexPath.row];
    
    supdetailVC.itemId = [smallModel.itemId stringValue];
    
    [self.navigationController pushViewController:supdetailVC animated:YES];
}

@end
